-
Notifications
You must be signed in to change notification settings - Fork 4
Feature decouple interfaces from pydantic #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Decouple Interfaces from Pydantic Types
📋 Summary
This PR introduces a major refactoring that decouples core interfaces (
IEvent,IRequest,IResponse) from Pydantic-specific implementations. The library now supports both Pydantic-based and Dataclass-based implementations, providing flexibility for users who want to avoid Pydantic dependency or prefer lightweight dataclass implementations.🎯 Motivation
Previously, the library was tightly coupled to Pydantic types, making it difficult for users who wanted to use alternative implementations. This refactoring:
🔄 Changes Overview
Core Interfaces
IEvent: Abstract interface for event-type objectsIRequest: Abstract interface for request-type objectsIResponse: Abstract interface for response-type objectsNew Implementations
Dataclass-based (Lightweight)
DCEvent: Dataclass implementation ofIEventDCDomainEvent: Dataclass implementation of domain eventsDCNotificationEvent: Dataclass implementation of notification eventsDCRequest: Dataclass implementation ofIRequestDCResponse: Dataclass implementation ofIResponsePydantic-based (Default, with validation)
PydanticEvent: Pydantic implementation ofIEvent(aliased asEvent)PydanticDomainEvent: Pydantic implementation of domain events (aliased asDomainEvent)PydanticNotificationEvent: Pydantic implementation of notification events (aliased asNotificationEvent)PydanticRequest: Pydantic implementation ofIRequest(aliased asRequest)PydanticResponse: Pydantic implementation ofIResponse(aliased asResponse)📁 Files Changed
Core Library Files (49 files, +1435/-172 lines)
Event System
src/cqrs/events/event.py- IntroducedIEventinterface and implementationssrc/cqrs/events/__init__.py- Updated exportssrc/cqrs/events/event_emitter.py- Updated to work withIEventsrc/cqrs/events/event_handler.py- Updated to work withIEventsrc/cqrs/events/event_processor.py- Updated to work withIEventsrc/cqrs/events/map.py- Updated to work withIEventRequest/Response System
src/cqrs/requests/request.py- IntroducedIRequestinterface and implementationssrc/cqrs/response.py- IntroducedIResponseinterface and implementationssrc/cqrs/requests/request_handler.py- Updated to work with interfacessrc/cqrs/requests/map.py- Updated to work with interfacessrc/cqrs/requests/mermaid.py- Updated to work with interfacessrc/cqrs/requests/cor_request_handler.py- Updated to work with interfacesDispatchers
src/cqrs/dispatcher/event.py- Updated to work withIEventsrc/cqrs/dispatcher/request.py- Updated to work with interfacessrc/cqrs/dispatcher/streaming.py- Updated to work with interfacessrc/cqrs/dispatcher/models.py- Updated type hintsSerializers/Deserializers
src/cqrs/deserializers/json.py- Updated to work with interfacessrc/cqrs/deserializers/protobuf.py- Updated to work with interfacessrc/cqrs/deserializers/exceptions.py- Updated exception handlingsrc/cqrs/serializers/default.py- Updated to work with interfacessrc/cqrs/serializers/protobuf.py- Updated to work with interfacesOther Components
src/cqrs/mediator.py- Updated to work with interfacessrc/cqrs/producer.py- Updated to work withIEventsrc/cqrs/outbox/*- Updated outbox components to work withIEventsrc/cqrs/message_brokers/protocol.py- Updated protocol definitionssrc/cqrs/middlewares/logging.py- Updated middlewaresrc/cqrs/container/dependency_injector.py- Updated DI integrationsrc/cqrs/saga/step.py- Updated saga stepssrc/cqrs/saga/recovery.py- Updated saga recoverysrc/cqrs/saga/mermaid.py- Updated saga visualizationsrc/cqrs/types.py- Updated type definitionssrc/cqrs/__init__.py- Updated public API exportsExamples
examples/request_response_types.py- NEW: Comprehensive example demonstrating different request/response type combinationsexamples/kafka_event_consuming.py- Updated to use interfacesexamples/saga_fastapi_sse.py- Updated to use interfacesTests
tests/unit/test_deserializers.py- Updated tests for new interfacestests/unit/test_event_processor.py- Updated teststests/unit/test_cor_request_handler.py- Updated teststests/unit/test_request_mediator_parallel_events.py- Updated teststests/unit/test_streaming_dispatcher.py- Updated teststests/unit/test_streaming_mediator.py- Updated teststests/integration/test_event_outbox.py- Updated integration teststests/integration/test_streaming_mediator.py- Updated integration testsDocumentation
README.md- Updated documentation with new interfaces and examplespyproject.toml- Updated project metadata if needed✨ Key Features
1. Interface-Based Design
All core types now implement abstract interfaces, allowing for multiple implementations:
2. Multiple Implementation Options
Pydantic (Default):
Dataclass (Lightweight):
3. Mix and Match Support
Users can combine different types based on their needs:
4. Backward Compatibility
Request,Response,Event) still point to Pydantic implementations🧪 Testing
All existing tests have been updated to work with the new interface-based design. The test suite covers:
📚 Documentation
examples/request_response_types.py) demonstrating:🔧 Migration Guide
For Existing Users
No changes required! The library maintains backward compatibility:
For New Users Wanting Dataclass Support
🎁 Benefits
📊 Statistics
examples/request_response_types.py)✅ Checklist
IEvent,IRequest,IResponse)🔗 Related Issues
This PR addresses the need for decoupling from Pydantic while maintaining full backward compatibility and adding flexibility for users who prefer alternative implementations.
Note: This is a significant refactoring that improves the library's architecture while maintaining backward compatibility. All existing functionality remains intact, and new options are available for users who want them.